Transparent user mode scheduling on traditional threading systems

ABSTRACT

Embodiments for performing cooperative user mode scheduling between user mode schedulable (UMS) threads and primary threads are disclosed. In accordance with one embodiment, an asynchronous procedure call (APC) is received on a kernel portion of a user mode schedulable (UMS) thread. The status of the UMS thread as it is being processed in a multi-processor environment is determined. Based on the determined status, the APC is processed on the UMS thread.

CROSS REFERENCE TO RELATED APPLICATIONS

This application is related to concurrently-filed U.S. patentapplication Ser. No. 12/242,566, entitled “Intelligent Context Migrationfor User Mode Scheduling,” which is incorporated herein by reference.

BACKGROUND

User mode scheduling enables processes to switch user threads for betterconcurrency and control. User mode schedulable (UMS) threads are dividedinto distinct user and kernel portions. However, the division of a UMSthreads into a user portion and kernel portion may cause difficultiesduring thread execution.

SUMMARY

This Summary is provided to introduce a selection of concepts in asimplified form that is further described below in the DetailedDescription. This Summary is not intended to identify key features oressential features of the claimed subject matter, nor is it intended tobe used to limit the scope of the claimed subject matter.

Described herein are embodiments of various technologies for cooperativeuser mode scheduling for the proper execution of UMS threads. In oneembodiment, an asynchronous procedure call (APC) is received on a kernelportion of a user mode schedulable (UMS) thread. The status of the UMSthread as it is being processed in a multi-processor environment isdetermined. Based on the determined status, the APC is processed on theUMS thread. Other embodiments will become more apparent from thefollowing detailed description when taken in conjunction with theaccompanying drawings.

BRIEF DESCRIPTION OF THE DRAWINGS

The detailed description is described with reference to the accompanyingfigures. In the figures, the left-most digit(s) of a reference numberidentifies the figure in which the reference number first appears. Theuse of the same reference number in different figures indicates similaror identical items.

FIG. 1 is a block diagram illustrating an exemplary multi-processorenvironment for the execution of user mode schedulable (UMS) threads, inaccordance with various embodiments.

FIGS. 2 a and 2 b are block diagrams illustrating the execution of auser mode schedulable (UMS) thread in the user and kernel modes via theuse of a primary thread, in accordance with various embodiments.

FIG. 3 illustrates the dual mapping of the user thread descriptor of aprimary thread for the purpose of facilitating the execution of aprimary thread, such as the primary thread 106, in accordance withvarious embodiments.

FIG. 4 illustrates the handling of privileged hardware states during theswitch of a primary thread and a UMS thread in user mode, in accordancewith various embodiments.

FIGS. 5 a-5 f illustrate the occurrence of an asynchronous procedurecall (APC) event during the execution of user mode schedulable (UMS)threads, in accordance with various embodiments.

FIG. 6 illustrates the use of a UMS lock that includes a clientidentifier (CID) and a lock bit during the switching between the userportions of a primary thread and a UMS thread, in accordance withvarious embodiments.

FIG. 7 is a block diagram illustrating selected components of amulti-processor environment that implements mechanisms of user modeschedulable (UMS) threads execution, in accordance with variousembodiments.

FIGS. 8 a and 8 b are flow diagrams illustrating an exemplary processfor dealing with the occurrence of an asynchronous procedure call (APC)during the execution of a UMS thread, in accordance with variousembodiments.

FIG. 9 is a flow diagram illustrating an exemplary process controllingthe execution of a UMS thread in user mode, in accordance with variousembodiments.

FIG. 10 is a flow diagram illustrating an exemplary process controllingthe yield of a UMS thread in user mode, in accordance with variousembodiments.

FIG. 11 is a block diagram illustrating a representative computingdevice. The representative device may be a part of a multi-processorenvironment, in accordance with various embodiments.

DETAILED DESCRIPTION

This disclosure is directed to embodiments of directed switches for theproper execution of user mode schedulable (UMS) threads. In amulti-processor environment, the UMS threads are program threads thatmay be executed by one or more central processor units (CPUs) in thecontext of an operating system process. The UMS threads are similar toprocessor threads currently in use on computing platforms in that eachUMS thread includes a kernel portion and a user portion.

The execution of a typical process thread may involve the recurringprocessing of the kernel portion of the thread in kernel mode, inconjunction with the recurring processing of the user portion in usermode, and the automatic switching between the two modes. In contrast,due to the unique architecture of the computing platform on which theUMS threads are executed, the UMS threads do not generally alternateautomatically between user and kernel modes. Instead, the user portionsof UMS threads are “switched” with the user portions of special threads,which are referred to herein as primary threads. In turn, the userportions of the primary threads may then be executed in user mode.However, this “substitution” of a user portion of a primary thread for auser portion of a UMS thread may create a mismatch in identities. Thisidentity mismatch between the user portion of the UMS thread and theprimary thread kernel portion may occur once the primary threadexecuting on behalf of the user portion of the UMS thread returns tokernel mode. Accordingly, a process referred to as a “directed switch”may be performed to remedy such mismatches and provide smooth transitionbetween the user and kernel portions of the UMS thread.

In some instances, the implementation of such “directed switches” maymake it necessary to implement certain support mechanisms and processes.The embodiments described herein are directed to these supportmechanisms and processes that work cooperatively to support directedswitches. Accordingly, various examples of these mechanisms andprocesses that support directed switches are described below withreference to FIGS. 1-11.

Exemplary Schemes

FIG. 1 is a block diagram illustrating an exemplary multi-processorenvironment 100 for the execution of user mode schedulable (UMS)threads, in accordance with various embodiments. As shown, the exemplarymulti-processor environment 100 may include one or more UMS threads,such as UMS thread 102 and 104, one or more primary threads, such asprimary threads 106 and 108, and one or more standard threads, such aprocess thread 110. In various embodiments, the process thread 110 maybe a NT thread that is part of the Windows NT® computing environment.

Each of the threads in the exemplary multi-processor environment 100comprises a kernel portion that resides in kernel mode 112, and a userportion that resides in user mode 114. The kernel and user modes arestandard ways of dividing the execution of threads in a computer systembetween operating system activities, such as activities performed by anoperating system (OS) kernel, and application activities, such as thoseperformed by a user's program.

For example, the UMS thread 102 may include a kernel portion 116 and auser portion 118. Likewise, the primary thread 106 may include a kernelportion 120 and a user portion 122. Additionally, the process thread 110may include a kernel portion 124 and a user portion 126. In variousembodiments, kernel mode 112 is implemented by the operating environmentfor a kernel of an operating system, i.e., the OS kernel. Kernel mode112 is generally a privileged mode of operation with direct access tothe hardware resources in the machine. Generally speaking, the OS kernelis responsible for managing a computing system's resources (i.e.,hardware and software components). As a basic component of an operatingsystem, the OS kernel provides the lowest-level abstraction layer forcomputing resources (e.g., memory, processors and I/O devices) that anapplication needs to control for the performance of various functions.

Moreover, the kernel and user portions of each thread in the exemplarymulti-processor environment 100 may also include additional memorystructure. For example, the kernel portion 116 may include a kernel modestack 128, registers 130, and a kernel structure that describes thekernel portion 116, which is the kernel thread descriptor 132.Furthermore, the user portion 118 may include a user thread descriptor134, which is a block of memory belonging to the user portion 118. Theuser portion 118 may further include registers 136, and a user modestack 138.

It will be appreciated that the UMS thread 102 is distinguishable from aprocess thread, such as the standard thread 110. In various embodiments,a UMS thread may differ from a standard thread in that the schedulingassignments of CPUs to the UMS kernel portion and the UMS user portionmay be independently managed by an operating system, while the kerneland user portions of a standard thread generally are assigned in unison.Thus, while switching between the kernel portion 124 and the userportion 126 of the standard thread 110 usually occurs implicitly duringthread execution on a central processing unit (CPU), the UMS thread mayprovide applications with the ability to control the switch between thevarious user portions of its threads in user mode during execution. Thiscapability allows applications to achieve higher performance andscalability because it is not always necessary to also switch the kernelportions of threads. For example, switching from a user portion to akernel portion of a standard thread 110 generally requires an entry intokernel mode, saving the old kernel state, accessing some kernel globaldata structures and locks, restoring new states, and exiting kernelmode. However, because UMS threads provide applications with the abilityto perform switches in user mode, the switches into kernel modes as wellas kernel data and/or lock contentions may be avoided.

The UMS threads, such as the UMS threads 102-104, may be created fromstandard threads, such as the standard thread 110, by allowing the userportion 126 and the kernel portion 124 to be assigned to CPUs, such asCPU 144 and CPU 146, independently. In embodiments where themulti-processor environment 100 is running on IA-64 (Itanium) CPUarchitecture, for example, the creation of a UMS thread may involve theuse of a special pointer 140 to the UMS states that are stored in userthread descriptor of the UMS thread. The special pointer 140 may bestored in a register that is accessible by other threads.

Alternatively, in embodiments where the multi-processor environment 100is running on an x86 or x64 CPU architecture, the creation of a UMSthread may involve the use of a segment selector 142 that points to theuser thread descriptor of the UMS thread. Accordingly, the specialpointer and/or the selector of a UMS thread may enable other threads,such as the primary threads, to access the states of the UMS thread, asstored in its corresponding user thread descriptor.

The primary threads, such as primary thread 106 and the 108, may act asvirtual processors for an application. In some embodiments, the numberof primary threads present in the multi-processor environment 100 maycorrelate with the number of CPUs present in the environment. Forexample, the primary thread 106 may execute on the CPU 144, while theprimary thread 108 may execute on the CPU 146. However, in otherembodiments, the number of primary threads present may be greater orfewer than the number of CPUs in the multi-processor environment 100.

A primary thread may act as virtual processor for executing the userportions of a UMS thread. For example, but not as a limitation, anapplication running the user portion 122 of primary thread 106 on theCPU 144 can switch execution to the user portion 118 of the UMS thread.The application can subsequently switch to executing other user portionsof the UMS threads by using its own scheduling policies without anyintervention or support from the OS kernel to implement its ownscheduling policies.

In various embodiments, a primary thread may be constructed from astandard thread, such as the standard thread 110, with the addition ofan extra state. Thus, a primary thread may convert back and forth from astandard thread based on application requirements. For example, anapplication can respond to resource expansion by constructing moreprimary threads and a decrease in resource pressure scenarios can resultin de-converting a primary thread back to a standard thread.

FIGS. 2 a and 2 b are block diagrams illustrating the execution of auser mode schedulable (UMS) thread in the user and kernel modes via theuse of a primary thread, in accordance with various embodiments. It willbe appreciated that while the FIGS. 2 a and 2 b are illustrated withrespect to a single primary thread 106, the multi-processor environment100 may include a plurality of primary threads running on multiple CPUs.

As shown in FIG. 2 a, the exemplary UMS thread 102 may include a kernelportion 116 that resides in kernel mode 112, and a user portion 118 thatresides in user mode 114. It will be appreciated that the execution of aUMS thread, such as the UMS thread 116, differs from the execution of astandard thread, such as the standard thread 110 (FIG. 1). Standardthread processing is generally dependent on the automatic, that is,implicit switch between a kernel portion of a thread and a user portionof a thread. For example, during the execution of the standard thread110 (FIG. 1), a CPU may process the kernel portion 124 of the standardthread 106 in kernel mode 112. Subsequently, the CPU may change fromkernel mode 112 to user mode 114 by automatically switching from thekernel portion 124 of the standard thread 110 to the user portion 126.The user portion 126 of the standard thread 110 is then furtherprocessed in user mode. Additionally, when the execution is to beswitched to the user portion of another standard thread, the currentuser portion 126 needs to switch to its kernel portion 124, which thenswitches to the kernel portion of the next standard thread to run. Thekernel portion of the next standard thread may also further switch toits user portion in the same manner.

However, during the processing of a UMS thread, the switch to the kernelportion of the UMS thread is generally not required to switch to theuser portion of another thread. The prevention of automatic switchingfrom a user portion to a kernel portion of a UMS thread duringprocessing is a fundamental principle of the UMS computing architecture.This architecture may enable computer applications to have greatercontrol over the concurrency and efficiency of threads in bothsingle-processor and multi-processor environments.

Instead, the UMS threads may use alternative implementations to performtasks in user mode 114. A particular alternative implementation isillustrated in FIG. 2 a with respect to the UMS thread 102. As shown, aCPU may in essence execute the user portion 118 of the UMS thread 102with the assistance of the primary thread 106. For example, the userportion 122 of the primary thread 106 may “switch” to the user portion118 of the UMS thread 102. The CPU may then execute the user portion 118of the UMS thread 102 in lieu of the user portion 122 of the primarythread 106.

In various embodiments, the “switch” of the user portion 122 to the userportion 118 may be accomplished by saving the context state of the userportion 118 and loading the context state of the user portion 122. In atleast one such embodiment, the saving and loading of context states ofthe user portion 118 to the user portion 122 includes switching the userthread descriptor 134 of the UMS thread 102 with a user threaddescriptor 202 of the primary thread 106 using one of the pointer 140 orthe segment selector 142 (FIG. 2 a). Moreover, the transfer may alsoinclude the switch of the context states from the registers 136,including the register pointing to the user mode stack 138 of the UMSthread 102, to the corresponding registers 204 and user mode stack 206of the primary thread 106. The switching of the user thread descriptorsand the various register and stack states is shown in FIG. 2 a as“context loading” 208 and “context saving” 210. Subsequently, the userportion 122 of the primary thread 106 may be executed using the sameCPU.

Following the execution of the user portion 122, the user threaddescriptors, registers, and user mode stacks of the user portion 122 andthe user portion 118, respectively, may be switched to enable executionof the user portion 118, or the user portion of yet another thread.These switches may be repeated one or more times. In other embodiments,the switching between the user portions of UMS threads may not need touse the user portion of the primary thread as an intermediary.

At some point in the execution of the user portion 118 of UMS thread 102in user mode 114 (via the use of the user portion 122), the UMS thread102 may need to enter kernel mode 112 from user mode 114. In otherwords, the user portion 118 of the UMS thread 102 may need to switch tothe kernel portion 116. In various instances, the need to enter kernelmode 112 may arise due to the occurrence of a system call (synchronousentry into kernel mode), an exception (asynchronous entry into kernelmode), or an interrupt (asynchronous entry into kernel mode). As usedherein, synchronous refers to a situation where entry into kernel modeis synchronized with the execution design of the user portion and thusonly a certain portion of the information in the register state 126(FIG. 1) of a UMS thread needs to be saved and later re-loaded, whileasynchronous refers to a situation where the execution of the userportion is interrupted essentially at a random point and thus all theinformation in the register state 126 needs to be saved and laterre-loaded. It will be appreciated that while system calls and exceptionsare thread-specific, interrupts are not specific to any thread.

For standard thread, such as standard thread 110 (FIG. 1), the switchfrom a user portion to a kernel portion may be accomplished implicitlywhen the standard thread may enters kernel mode 112 from user mode 114.To provide isolation between application and operating system execution,user mode 112 and kernel mode 114 provide special register state toreduce how much saving and loading is required when switching from theuser portion 126 of a standard thread 110 to the kernel portion 124,such as providing a separate register pointing to the kernel mode stack128. These special facilities in the CPU architecture make some of thestate save restore when switching between the user and kernel portionsof the same standard thread “implicit”. For example, upon entry of thestandard thread 110 into kernel mode 112, register state may beautomatically spilled to the implicit stack pointer and the new kernelstack pointer may be automatically loaded. Moreover, during the switchof the kernel portion 124 with the kernel portion of another standardthread (not shown), this special register state may be loaded (inaddition to a regular stack pointer) to point to the kernel stack areaof the new standard thread.

However, the switch from the user portion 118 of UMS thread 102 to thekernel portion 116 may need special steps that are not necessary for theimplicit switch from the user portion 126 to the kernel portion 124 of astandard thread 110 (FIG. 1). As shown in FIG. 2 a, the initial step ofthe switch from the user portion 118 to the kernel portion 116 of theUMS thread 102 is an implicit switch between the user portion 122 andthe kernel portion 120 of the primary thread 106. This is due to thefact that the user portion 118 was previously executing as the userportion 122 in user mode 114. Nevertheless, this “implicit switch” doesnot produce the desired result because the user portion 122 of theprimary thread 106 has been replaced by the user portion 118 of the UMSthread 102. In other words, as far as the OS kernel last knew the stateof what was running in user mode on the processor, it was the userportion 122 of the primary thread 106. This is because a switch fromuser portion 122 to the user portion 118 in the UMS thread 102 can occurwithout the awareness of the OS kernel. Thus, when the user portion 122enters kernel mode 112, the implicit switch will take it to the kernelportion 120 of the primary thread 106. However, since the user portion122 was really replaced by the user portion 118 of the UMS thread 102,the user portion 118 will actually enter kernel mode 112.

Accordingly, when the user portion 118 of UMS thread 102, not the userportion 122 of the primary thread 106, enters kernel mode 112, the OSkernel that is expecting the user portion 122 of primary thread 106 mayhave the ability to detect the mismatch based on the identities of eachuser portion. In various embodiments, the identity of each user portionof each thread, which is part of the context information of each thread,may be stored in the kernel thread descriptors. For example, theidentity of the UMS thread 102 may be stored in its kernel threaddescriptor 132, while the identity of the primary thread 106 may bestored in its kernel thread descriptor 212.

For correct operation of the system, the OS kernel needs to handle thiscase where the “implicit switch” will run the kernel portion of thewrong thread. This is because the operating system attaches certainimport information (such as the security context) to the kernel threaddescriptor. Since this state is only ever consumed in kernel mode 112,the UMS thread needs to run with the correct context once it enterskernel mode 112. Therefore, in the example of the UMS thread 102, the OSkernel may switch from the kernel portion 120 of the primary thread 106to the kernel portion 116 once the UMS thread 102 enters kernel mode112. This cross-thread switching process is referred to as a “directedswitch.” Thus, directed switches may enable UMS threads to be executedtransparently in an operating system, that is, in the same way asstandard threads.

In various embodiments, the directed switch may be accomplished byexchanging context information 214 of the kernel portion 120 (primarythread 106) with the context information 216 of the kernel portion 116(UMS thread 102). This process is shown as “context migration” 218. Asused throughout, “context information” refers to the state of a thread,which may include the registers that the thread may be using, as well asother operating system and hardware specific data necessary for theexecution of the thread. In this way, once the context migration 218 iscompleted, the kernel portion 116 may be executed by a CPU in kernelmode 112.

FIG. 2 b illustrates the switch of the UMS thread 102 back to user mode114 following the execution of the kernel portion 116 of the thread inkernel mode 112. As shown, the UMS thread 102 may switch back to usermode 114 by saving the context information of its user portion 118 thatwas saved by the OS kernel into some user mode accessible structure whenthe user portion previously entered kernel mode 112. For example, insome embodiments, the context information may be saved to the user modestack 138 of the user portion 118. In other embodiments, the contextinformation may be saved to the user thread descriptor 134.

Subsequently, the kernel portion 116 of the UMS thread 102 may cause thecontext information associated with user portion 118 of the UMS thread102 to be queued to a UMS completion list 202. The UMS completion list202 may be a UMS data structure on which the OS kernel queues contextinformation for user portions of the UMS threads that are ready forexecution in user mode 112. After the queuing of the user portion 118,the kernel portion 116 of the UMS thread 102 may place itself in a“parked” state (i.e., a type of ready waiting state).

Moreover, when freed by the UMS thread 102 following the kernel modecontext migration 218, the primary thread 106 may switch back to itsuser portion 122 in user mode 114. Once back in user mode 114, the userportion 122 primary 106 may switch to the user portion of another UMSthread for execution.

FIG. 3 illustrates the dual mapping of the user thread descriptor of aprimary thread for the purpose of facilitating the execution of aprimary thread, such as the primary thread 106, in accordance withvarious embodiments. The dual mapping is needed to accommodate the AMD64/Intel EM64T architecture. As described above with respect to FIG. 2,the execution of the UMS thread 102 in user mode 114 via the primarythread 106 relies on the ability to switch user thread descriptors ofthe primary thread and the UMS thread.

In various embodiments, a primary thread may be created from a standardthread 110 (FIG. 1), which may include NT threads. Moreover, the userthread descriptor of each primary thread is dynamically allocated by theOS kernel during the creation of the original standard thread 100. Thus,the user thread descriptor of a primary thread may reside in any part ofa User Virtual Address (VA) space. As shown in FIG. 3, the User VA space304 may include addresses above 4 GB (2^32), such as an upper 4 gigabyte(GB) portion 306 shown as well as a lower 4 GB portion 308.

Thus, in one example, the user thread descriptor 302 of a primary threadmay reside in an upper 4 gigabyte (GB) portion 304 of the User VA space306. Further, the user thread descriptor 302 may include a self-pointer310 that points to the beginning of the user thread descriptor 302. Forexample, but not as a limitation, if the user thread descriptor 302begins at hexadecimal memory address “B00000100”, the self pointer 310may point to the memory address “B00000100”. Moreover, the user threaddescriptor 302, as located in the User VA space 304, may map to the userthread descriptor pages 312 located in the physical memory 314. The selfpointer 310 may be used by various processes to access or refer to theuser thread descriptor via its flat linear address.

In various computing architectures, the user thread descriptor 302 maybe accessed, such as for the purpose of UMS thread switching, viadifferent structures. For example, but not as a limitation, IntelArchitecture, 32 bit (IA-32) and AMD64/Intel EM64T architectures mayaccess the user thread descriptor 302 via a data segment 316. The IntelArchitecture, 64 bit (IA-64), on the other hand, may access the userthread descriptor 302 via a general purpose register (not shown).

While the IA-32 and the IA-64 architectures need no specialaccommodations to access the user thread descriptor 302 that is storedin the User VA, the AMD 64/Intel EM64T architectures may pose specialconstraints. Specifically, due to historical limitations, the loading ofthe data segment 316 from user mode, is constrained to 32 bits (ratherthan the 64 bits that are loadable from kernel mode. The 32 bits of thedata segment 310 corresponds to the lower 4 GB 308 of the User VA space306. As a result, when a primary thread includes the user threaddescriptor 302 that is located in the upper 4 GB 304, the user threaddescriptor 302 is inaccessible by the data segment 310.

Accordingly, in such a scenario, a page unit 318 may be used to generatean image of the user thread descriptor 302 in the lower 4 GB portion308. In other words, a second mapping to the user thread descriptorpages 312, referred to in FIG. 3 as secondary user thread descriptor320, may be created in the lower 4 GB portion 308. Moreover, it will beappreciated that while a self pointer 322, which is a mirror image ofthe self pointer 310, may reside in the secondary user thread descriptor320. It nevertheless points to the beginning of the user threaddescriptor 302. For example, but not as a limitation, even if thesecondary user thread descriptor 320 begins at memory address “20” inthe example, the self pointer 320 still points to the memory address“B00000100”. As a result, regardless of whether the data segment 316 isloaded from kernel mode 112 or user mode 114, user thread descriptoraccess operations will always lead to the same actual user threaddescriptor pages 312 in the physical memory 314.

In various embodiments, the secondary user thread descriptor 320 may beremoved from the User VA space 304 when it has been established that theaddress of the secondary user thread descriptor 320 is not being used,and no further switches of the primary thread that corresponds to thesecondary user thread descriptor 320 will occur.

FIG. 4 illustrates the handling of privileged hardware states during theswitch of a primary thread and a UMS thread in user mode, in accordancewith various embodiments. It will be appreciated that while the handlingof the privileged hardware states is illustrated using the UMS thread102 and the primary thread 106, the same principles hold for other UMSthread and primary threads.

As described above, during the switching of the user portion 122 of theprimary thread 106 with the user portion 118 of the UMS thread 102, theuser thread descriptor and registers of each thread may be switched,that is, swapped. Nevertheless, the switch of certain privilege hardwarestates that are not stored in the user thread descriptor and theregisters of the UMS thread 102 is generally not allowed in user mode114. For example, the loading of the hardware debug registers 402 of theUMS thread 102, which may include privileged hardware states, into theprimary thread 106, is not permitted in user mode 114.

As shown in FIG. 4, when debug registers 402 are enabled for the UMSthread 102, the updated register states of the UMS thread 102 arecaptured and stored in a context holder 404 of the user portion 118.Thus, when the primary thread 106 switches to the UMS thread 102 in usermode 112, the primary thread 106 may detect that the debug values arestored in the context holder 404 of the user portion 118. In variousembodiments, the context holders 404 may include the user mode stacks138 as described in FIG. 2 a.

Subsequently, the primary thread 106 may invoke a kernel call 406 toenter kernel mode 112. Once in kernel mode 112, the primary thread 106may change its own thread state to reflect the kernel states of the UMSthread 102. In various embodiments, the primary thread 106 mayaccomplish the thread state change by exchanging its context informationwith the context information of the UMS thread 102 via a contextmigration 402. Following the thread state change, the primary thread 106may load the hardware debug registers 402 into its kernel portion 120.

FIG. 5 a illustrates the occurrence of an asynchronous procedure call(APC) event 502 (a type of signal event) during the execution of thekernel portion of a UMS thread in kernel mode, in accordance withvarious embodiments. The execution of the kernel portion of the UMSthread may be a part of the overall execution of the UMS thread in boththe kernel and user mode, as previously described in FIGS. 2 a and 2 b.

An APC can perform various actions, including collecting and/or changingthe context of thread. Moreover, since APCs are asynchronous, they mayoccur at any time during the execution of UMS thread via a primarythread. In various embodiments, the APCs are generally delivered to thekernel portions of threads in kernel mode 112. However, the APCs mayimpact the execution of threads in user mode 114. For example, but notas limitation, the APCs may be used to obtain the context of the userportions of threads, to suspend the execution of the user portions ofthreads, and to terminate the execution of the user portion of threads.It will be appreciated that while the occurrence of the APC 502 isillustrated using the UMS thread 102 and the primary thread 106, thesame principles hold for other UMS threads and primary threads.

As shown in FIG. 5 a, an APC 502 may be fired into kernel mode 112 whenthe kernel portion 116 of the UMS thread 102 is being executed in kernelmode 112. Accordingly, the APC 502 may run on the kernel portion 116.For example, the APC 502 may retrieve the context information from thekernel portion 116. In another example, the APC 502 may modify thecontext information in the kernel portion 116.

FIG. 5 b illustrates the occurrence of an asynchronous procedure call(APC) event 504 when the kernel portion and user portions of a UMSthread are not being executed, in accordance with various embodiments.It will be appreciated that while the occurrence of an APC isillustrated using the UMS thread 102 and the primary thread 106, thesame principles hold for other UMS threads and primary threads.

As shown, the kernel portion 116 of the UMS thread 102 is not beingexecuted in kernel mode 112 when the APC 504 is delivered to the UMSthread 102 in kernel mode. Upon receiving the APC 504, the kernelportion 116 may first cause a “kernel lock” to be placed on the userportion 118. In various embodiments, the “kernel lock” may be theperformed using the lock bit 606 further described below in FIG. 6. The“kernel lock” may prevent the states of the user portion 118 from beingmodified by another mechanism, such as by a user mode scheduler. Invarious embodiments, the “kernel lock” may refer to a soft lock thatdoes not prevent the user states of the user portion 118 from beingaccessed by a non-cooperative code.

Second, the kernel portion 116 of the UMS thread 102 may peek at theuser portion 118 of the thread to determine whether the user portion 118is currently being executed. In at least one embodiment, the UMS thread102 may look at a specific context field of the user portion 118 todetermine whether it is already being executed. If the kernel portion116 determines that the user portion 118 is idle and not being executed,the kernel portion 116 may enable the APC 504 to access the user portion118. For example, the APC 504 may retrieve the context information fromthe user portion 118. In another example, the APC 502 may modify thecontext information in the user portion 118.

FIG. 5 c illustrates the occurrence of an asynchronous procedure call(APC) event 506 when the user portion of a UMS thread is being executedin user mode via a primary thread, in accordance with variousembodiments. It will be appreciated that while the occurrence of the APC506 is illustrated using the UMS thread 102 and the primary thread 106,the same principles hold for other UMS threads and primary threads.

As shown, the kernel portion 116 of the UMS thread 102 is not beingexecuted in kernel mode 112 when the APC 506 is delivered to the UMSthread 102 in kernel mode 112. Upon receiving the APC 506, the kernelportion 116 may first cause a “kernel lock” to be placed on the userportion 118. In various embodiments, the “kernel lock” may be theperformed using the lock bit 606 further described below in FIG. 6.

Second, the kernel portion 116 of the UMS thread 102 may peek at theuser portion 118 of the thread to determine whether the user portion 118is currently being executed. In at least one embodiment, the UMS thread102 may look at a specific context field of the user portion 118 todetermine whether it has been locked for execution, such as execution bya primary thread. As further described below, when the user portion 118is locked for execution by a primary thread, the corresponding “lock”may include a client identifier (CID) 508 that identifies the particularprimary thread. For example, the CID 508 may identify the particularprimary thread as the primary thread 106.

Thus, once the kernel portion 116 determines that the user portion 118is currently locked for execution by the primary thread 106 (e.g.,switched with the user portion 122), the kernel portion 116 may fire adisassociate APC 510 to the primary thread 106, as identified by the CID508.

In various embodiments, assuming that the user portion 122 of theprimary thread 106 is being executed in user mode 114, the disassociateAPC 510 may cause the primary thread 106 to abandon its execution inuser mode 114 and return to kernel mode 112. Moreover, once the primarythread 106 is in kernel mode 114, the primary thread 106 may return thecontext information from its kernel portion 120 to the user portion 118of the UMS thread 102. Having returned the context information, theprimary thread 106 may return to user mode 114. In other words, theprimary thread 106 may switch to its user portion 122, which mayeventually pick up another UMS thread user portion for execution.Further, the kernel portion 116 may enable the APC 506 to access theuser portion 118. For example, the APC 506 may retrieve the contextinformation from the user portion 118. In another example, the APC 506may modify the context information in the user portion 118.

FIG. 5 d illustrates the occurrence of an asynchronous procedure call(APC) event 512 when a context migration from a primary thread to a UMSthread, or a directed switch, is being performed in kernel mode, inaccordance with various embodiments. It will be appreciated that whilethe occurrence of the APC 512 is illustrated using the UMS thread 102and the primary thread 106, the same principles hold for other UMSthreads and primary threads.

As shown, the kernel portion 116 of the UMS thread 102 is not beingexecuted in kernel mode 112 when the APC 512 is delivered to the UMSthread 102 in kernel mode. Upon receiving the APC 512, the kernelportion 116 may first cause a “kernel lock” to be placed on the userportion 118. In various embodiments, the “kernel lock” may be theperformed using the lock bit 606 further described below in FIG. 6.Second, the kernel portion 116 of the UMS thread 102 may peek at theuser portion 118 of the thread to determine whether the user portion 118is currently being executed. In at least one embodiment, the UMS thread102 may look at a specific context field of the user portion 118 todetermine whether it has been locked for execution, such as execution bya primary thread. As further described below, when the user portion 118is locked for execution by a primary thread, the corresponding “lock”may include a client identifier (CID) 514 that identifies the particularprimary thread. For example, the CID 514 may identify the particularprimary thread as the primary thread 106.

Thus, once the kernel portion 116 determines that the user portion 118is currently locked for execution by the primary thread 106 (e.g.,switched with the user portion 122), the kernel portion 116 may fire adisassociate APC 516 to the primary thread 106, as identified by the CID514.

In various embodiments, assuming that the kernel portion 120 of theprimary thread 106 is running in kernel mode 112, the kernel portion 120may be in the process of “context migration 218” (FIG. 2 a) when thedisassociate APC 516 is fired. As described above, “context migration”218 may include the transfer of context information 214 from the kernelportion 120 to the kernel portion 116 of the UMS thread 102. In variousinstances, this may create a blocking condition as the kernel portion116 may be waiting for the disassociate APC 516 to be implemented by thekernel portion 120. In turn, the kernel portion 120 may be unable todisassociate as it is already in the process of transferring state(i.e., the most recent/accurate UMS thread state is actually in thekernel portion 116, not in the user thread descriptor 202 of userportion 122).

In order to alleviate this blocking condition, the primary thread 106may send a “defer” message to the kernel portion 116 of the UMS thread102 in response to the disassociate APC 516. The “defer” message mayinform the UMS thread 102 that the primary thread 106 is attempting toswitch to the UMS thread 102. In response to the “defer” message, aroutine 518 that is created for and enables the APC 512 to pull thecontext information into kernel portion 116 may be temporarily renderedinactive. In the place of routine 518, the disassociate APC 516 maycreate a function 520 in the kernel portion 116 that waits for anindicator 522 from the primary thread 106. The indicator 522 may beconfigured to show that the context information has become ready totransfer from the kernel portion to the kernel portion 116. Thus, whenthe kernel portion 120 signals the UMS thread 102 with the indicator522, the original APC 512 may “pull” the context information from theuser portion 118 into a staging area 524 that is in the kernel portion116. This staging area enables the primary thread 106 to be informedthat its part in the “directed switch” operation is completed, evenbefore running the actual APC 512. Once the original APC 512 completesthe pull, the routine 518 may pull the context information from thestaging area 524 and replace the original context information of thekernel portion 116, such as context information 216 (FIG. 2 a), with thecontext information from kernel portion 120. It will be appreciated thatthe staging area 524 is necessary as the APC 516 may result in asuspension of the UMS thread 102. Thus, the primary thread 106 should bereleased prior to this suspension, thereby necessitating the stagingarea 524 as a safe temporary storage area for the context information inthe event of a suspension

Further, since the appropriate context information is in the kernelportion 116, the kernel portion 116 may enable the APC 512 to run on thekernel portion 116. For example, the APC 512 may retrieve the contextinformation from the kernel portion 116. In another example, the APC 512may modify the context information in the kernel portion 116.

FIG. 5 e illustrates the occurrence of an asynchronous procedure call(APC) event 526 when a context loading from the UMS thread to a primarythread is being performed in user mode, in accordance with variousembodiments. It will be appreciated that while the occurrence of the APC526 is illustrated using the UMS thread 102 and the primary thread 106,the same principles hold for other UMS threads and primary threads.

As shown, the kernel portion 116 of the UMS thread 102 is not beingexecuted in kernel mode 112 when the APC 526 is delivered to the UMSthread 102 in kernel mode. Upon receiving the APC 526, the kernelportion 116 may first cause a “kernel lock” to be placed on the userportion 118. In various embodiments, the “kernel lock” may be theperformed using the lock bit 606 further described below in FIG. 6.Second, the kernel portion 116 of the UMS thread 102 may peek at theuser portion 118 of the thread to determine whether the user portion 118is currently being executed. In at least one embodiment, the UMS thread102 may look at a specific context field of the user portion 118 todetermine whether it has been locked for execution, such as execution bya primary thread. As further described below, when the user portion 118is locked for execution by a primary thread, the corresponding “lock”may include a client identifier (CID) 528 that identifies the particularprimary thread. For example, the CID 528 may identify the particularprimary thread as the primary thread 106. Thus, once the kernel portion116 determines that the user portion 118 is currently locked forexecution by the primary thread 106 (e.g., switched with the userportion 122), the kernel portion 116 may fire a disassociate APC 530 tothe primary thread 106, as identified by the CID 528.

As further shown, a switch between the primary thread 106 (user portion122) and the UMS thread 102 (user portion 118) may be in the middle ofbeing performed when the disassociate APC 530 is fired. Accordingly, theprimary thread 106 may be in the process of collecting contextinformation from the user portion 118. For example, this collection isdepicted in FIG. 2 a as context loading 208. Thus, during the occurrenceof the disassociated APC 530, it is possibly that a portion of thecontext information belonging to the UMS thread 102 is still in thememory (e.g., registers 136) associated with the user portion 118, whilethe remainder of this context information has been transferred to theprocessor executing the user portion 122, such as CPU 144. Thisprogression of context loading 208 may be detected by the OS kernel asthe disassociate APC 530 is delivered to the UMS thread 102 in kernelmode 112.

In response to the detection that a switching function was in progresswhen APC 530 or a page fault (a type of asynchronous event) occurs, theOS kernel may complete the context loading on behalf of the user portion122. In at least one embodiment, the OS kernel may finish the collectionof context information regarding the user portion 118 into the memoryassociated with the primary thread 106. Once the context loading iscomplete, the kernel portion 116 may enable the APC 526 to access theuser portion 118. For example, the APC 526 may retrieve the contextinformation from the user portion 118. In another example, the APC 526may modify the context information in the user portion 118.

FIG. 5 f illustrates the occurrence of an asynchronous procedure call(APC) event 532 when a context saving from the primary thread to the UMSthread is being performed in user mode in accordance with variousembodiments. It will be appreciated that while the occurrence of the APC532 is illustrated using the UMS thread 102 and the primary thread 106,the same principles hold for other UMS threads and primary threads.

As shown, the kernel portion 116 of the UMS thread 102 is not beingexecuted in kernel mode 112 when the APC 532 is delivered to the UMSthread 102 in kernel mode. Upon receiving the APC 532, the kernelportion 116 may first cause a “kernel lock” to be placed on the userportion 118. In various embodiments, the “kernel lock” may be theperformed using the lock bit 606 further described below in FIG. 6.Second, the kernel portion 116 of the UMS thread 102 may peek at theuser portion 118 of the thread to determine whether the user portion 118is currently being executed. In at least one embodiment, the UMS thread102 may look at a specific context field of the user portion 118 todetermine whether it has been locked for execution, such as execution bya primary thread. As further described below, when the user portion 118is locked for execution by a primary thread, the corresponding “lock”may include a client identifier (CID) 534 that identifies the particularprimary thread. For example, the CID 534 may identify the particularprimary thread as the primary thread 106. Thus, once the kernel portion116 determines that the user portion 118 is currently locked forexecution by the primary thread 106 (e.g., switched with the userportion 122), the kernel portion 116 may fire a disassociate APC 536 tothe primary thread 106, as identified by the CID 534.

As further shown, the disassociate APC 536 may have occurred when theprimary thread 106 is yielding to the UMS thread 102 after it isexecuted in user mode 114 Accordingly, the primary thread 106 may be inthe process of depositing context information regarding the user portion118 during the occurrence of the disassociate APC 536. For example, thisdeposit is depicted in FIG. 2 a as context saving 210. Thus, during theoccurrence of the disassociate APC 536, it is possibly that a portion ofthe most recently processed context information is already in the memoryimage associated with the user portion 118, while the remainder of thiscontext information is still present in the processor executing the userportion 122, such as CPU 144. As used herein, the most recentlyprocessed context information refers to context information produced bythe most recent execution of the primary thread 106 on a CPU. Thisprogression of context saving 210 may be detected by the kernel as thedisassociate APC 536 is delivered to the UMS thread 102 in kernel mode112.

In response to the detection that a yield was in progress when thedisassociate APC 536 or a page fault (a type of asynchronous event)occurs, the OS kernel may complete the current context saving 210 sothat the user portion 118 contains the states immediately prior to theoccurrence of the context saving 210. Once the states of the userportion 118 are saved, the kernel portion 116 may enable the APC 532 toaccess the user portion 118. For example, the APC 532 may retrieve thecontext information from the user portion 118. In another example, theAPC 532 may modify the context information in the user portion 118.However, in various embodiments, while the context saving 210 may becompleted, an instruction pointer (e.g., CPU register) that indicatesthe progress of the yield process may be reset to a starting point.Accordingly, the instruction pointer may enforce the yield process torestart from the beginning. Thus, when the APC 532 is completed, thecontext saving 210 from the primary thread 106 to the UMS thread 102 maybe re-run.

FIG. 6 illustrates the use of a UMS lock 602 that includes a clientidentifier (CID) 604 field and lock bit 606 during the switching betweena primary thread and a UMS thread, in accordance with variousembodiments. As described above, the CID is further used to identify aprimary thread that has switched to a UMS thread during the occurrenceof an asynchronous procedure call (APC).

As shown, each UMS thread, such as the UMS thread 102, may include a UMSlock 602. In turn, the UMS lock 602 may include a CID field 604 and alock bit 606. The CID field 604 field is part of a system data structurethat identifies each process and thread in the process with a uniqueidentifier. The lock bit 606 may have a default value when the UMSthread 102 is not being executed by the OS kernel in kernel mode 112.For example, but not as a limitation, the default value of the lock bit606 may be “zero”. Moreover, the CID field 604 may also have a defaultvalue when a primary thread has not switched to the UMS thread 102. Forexample, but not as a limitation, the default value of the CID field 604may also be “zero.”

Thus, when the primary thread 106 attempts to switch to the UMS thread102, it may first check to ensure that the CID field 604 and the lockbit 606 both have their respective default values. In other words, theprimary thread 106 may verify that the UMS thread 102 is not currentlybeing executed and no other primary thread has previously switched tothe UMS thread 102. If the primary thread 106 is able to verify thatboth default values are present, the primary thread 106 may replace thedefault value of the CID field 604 with its own identification 608.

Subsequently, the primary thread 106 may perform a switch with the UMSthread 102. As part of the switch process, the primary thread 106 mayreplace its user thread descriptor 202 with the user thread descriptor134 of the UMS thread 102, as described above. Once the primary thread106 has replaced its user thread descriptor, the scheduler may onceagain test the lock bit 606 to ensure that it still holds the defaultvalue, meaning that it has not been locked by the OS kernel. The reasonfor the second lock bit test is that the occurrence of an APC followingthe first lock bit test and during the switch of the primary thread 106may cause the UMS thread 102 to be executed by the OS kernel. If thesecond lock bit test indicates that the lock bit 602 still retains itsdefault value, the primary thread 106 may further replace its ownregisters 204 with the registers 136 of the UMS thread 102. Once theregisters are in place, the primary 106 may be executed in user mode 114in place of the UMS thread 102. Thus, any interruption of the primarythread 106 beyond this point follows the scenario in FIG. 5 f since theprimary thread 106 is now fully associated with the UMS thread 102. Onthe other hand, if the lock bit 602 is set, a switch to kernel mode 112is likely to follow and the primary thread 106 needs to select andswitch to another UMS thread.

Exemplary Components

FIG. 7 illustrates selected components of a multi-processor environment100. The selected components may include computer-program instructionsbeing executed by a computing device, such as the computing device 1200described in FIG. 12. The selected components may be stored in memory702 and act on one or more processors 704. The memory 702 may includevolatile and nonvolatile memory, removable and non-removable mediaimplemented in any method or technology for storage of information, suchas computer-readable instructions, data structures, program modules orother data. Such memory may include, but is not limited to, randomaccess memory (RAM), read-only memory (ROM), electrically erasableprogrammable read-only memory (EEPROM), flash memory or other memorytechnology, CD-ROM, digital versatile disks (DVD) or other opticalstorage, magnetic cassettes, magnetic tape, magnetic disk storage orother magnetic storage devices, RAID storage systems, or any othermedium which can be used to store the desired information and isaccessible by a computer system.

The selected components stored in memory 704 may in the form of programinstructions. The program instructions, or modules, may includeroutines, programs, objects, components, and data structures thatperform particular tasks or implement particular abstract data types.The modules may be implemented as software or computer-executableinstructions that are executed by one or more processors 702. As shownin FIG. 7, the selected components may include a user mode scheduler706, a kernel scheduler 708, and a kernel queue 710. However, it will beappreciated that the environment may also include other modules thatperform other functions related to thread execution.

The user mode scheduler 706 may be configured to create primary threadsand manage UMS threads. The user mode scheduler 706 may be furtherresponsible for exposing UMS thread mechanisms to higher levelcomponents in applications, and may also control and implementscheduling policies specific to an application and its instances thatuses the threads. The user mode scheduler 706 may include a primarythread API module 712, a UMS thread API module 714, a UMS completionlist API module 716, and an accessory API module 718. The primary threadAPI module 712 may be configured to enable an application to start a newinstance of the user mode scheduler 706. The UMS thread API module 714may be configured to create a new UMS thread, such as the UMS thread102, from a standard thread, such as the standard thread 110. In one atleast one embodiments, the thread API module 714 may convert a standardthread into a new UMS thread by adding a special state, or convert astandard thread into a primary thread and vice versa.

Moreover, the thread API module 714 may enable a primary thread toswitch to a UMS thread. Additionally, the thread API module 714 mayfurther enable a first UMS thread to yield to a second UMS thread byproviding a hint to the primary thread, such as the primary thread 106,which indicates the UMS thread to execute as a result of the yield.Finally, the thread API module 714 may be implemented to convert a UMSthread or a primary thread back into a standard thread with the removalof the special state.

The UMS completion list API module 716 may be configured to create andmanage a UMS completion list, such as the UMS completion list 202. Inother embodiments, the UMS completion list API module 716 may withdrawnfrom the UMS completion list all queued UMS thread user portions. Inadditional embodiments, the UMS completion list API module 716 may beimplemented to delete a UMS completion list.

The accessory API module 718 may be configured to fetch various UMSthread states. For example, the accessory API module 718 may fetch a UMSthread user portion from a UMS completion list so that it may beswitched with a user portion of a primary thread for execution on a CPU.In various embodiments, the accessory API module 718 may obtain thelatest queued UMS thread from the UMS completion list, as well as obtainthe next queued UMS thread. In other embodiments, the accessory APImodule 718 may be further configured to provide each of the UMS threadswith thread information, as well as retrieve thread information fromeach of the UMS threads.

The kernel scheduler 708 may be configured to schedule the variousthreads for processing on a CPU. In various embodiments, the schedulingpolicy of the kernel scheduler 708 may include preemptive,priority-based, and round robin processing. For example, the UMS threads(e.g., the UMS thread 102), the primary threads (e.g., the primarythread 106), as well as the standard threads (e.g., standard processthread 110), may use the services of the kernel scheduler 708 to getappropriately scheduled and pre-empted.

Finally, the kernel queue 710 is a mechanism that may be configured tomonitor system operations and registers events and notify appropriatecomponents of changes. The kernel queue 610 may also used by the kernelto control the concurrency level of threads associated with a particularqueue. In various embodiments, the kernel queues 710 may wake up theprimary thread, such as the primary thread 106, when the user portion118 of the UMS thread 102 enters into a “parked” state or otherwiseblocks in kernel mode 112 (e.g., due to a synchronous wait in a systemcall or a page fault). This may be accomplished by ensuring that thekernel queues used with the UMS mechanism only allow a concurrency levelof a single thread.

Exemplary Processes

FIGS. 8-10 illustrate exemplary processes that are used during theexecution of the user mode schedulable (UMS) threads, in accordance withvarious embodiments. The exemplary processes in FIGS. 8-10 areillustrated as a collection of blocks in a logical flow diagram, whichrepresents a sequence of operations that can be implemented in hardware,software, and a combination thereof. In the context of software, theblocks represent computer-executable instructions that, when executed byone or more processors, perform the recited operations. Generally,computer-executable instructions include routines, programs, objects,components, data structures, and the like that perform particularfunctions or implement particular abstract data types. The order inwhich the operations are described is not intended to be construed as alimitation, and any number of the described blocks can be combined inany order and/or in parallel to implement the process. For discussionpurposes, the processes are described with reference to the exemplarymulti-processor environment 100 of FIG. 1, although they may beimplemented in other system architectures.

FIGS. 8 a and 8 b are flow diagrams illustrating an exemplary process800 for dealing with the occurrence of an asynchronous procedure call(APC) during the execution of a UMS thread, in accordance with variousembodiments. At block 802, an APC may be delivered to a thread that isbeing executed. It will be appreciated that while the process 800 isillustrated using the UMS thread 102 and the primary thread 106, theprocess 800 may be applicable to other UMS threads and primary threads.

At decision block 804, the OS kernel may determine whether the APC isdelivered to a UMS thread, such as the UMS thread 102. If the OS kerneldetermines that the APC is not delivered to a UMS thread 102 (“no” atdecision block 804), the process 800 may proceed to block 806. Invarious embodiments, the APC may have been delivered to a standardthread, such as standard thread 100. At block 806, the APC may beprocessed as part of standard thread processing, such as the processingof a NT thread. However, if the OS kernel determines that the APC isdelivered to a UMS thread (“yes” at decision block 804), the process 800may proceed to decision block 808. In various embodiments, the APC maybe delivered to the kernel portion 116 of a UMS thread 102.

At decision block 808, the OS kernel may determine whether the kernelportion 116 of the UMS thread 102 is in a parked state. In variousembodiments, as described above, once the execution of a user portion ofthe UMS thread 102 is completed in kernel mode 112, the kernel portion116 of the UMS thread 102 may place itself in a “parked” state (i.e., atype of ready waiting state that enables the kernel portion 116 to waitfor the occurrence of a directed switch). Thus, conversely, the fact thekernel portion 116 is not in a parked state indicates that the kernelportion 116 is still being executed.

If the OS kernel determines that the kernel portion 116 is not in aparked state (“no” at decision block 808), the process 800 may proceedto block 806. At block 806, the APC may be processed as part of standardthread processing, such as the processing of a NT thread. However, ifthe OS kernel determines that the kernel portion 116 is in a parkedstated (“yes” at decision block 808), the process 800 may proceed todecision block 810.

At decision block 810, the OS kernel may determine whether the userportion 118 of the UMS thread 102 is running in the primary thread 106.For example, the user portion 118 may be switched with the user portion122 of the primary thread 106 for execution in user mode 114. If the OSkernel determines that the user portion 118 is not running in primarythread 106 (“no” at decision block 810), the process 800 may proceed todecision block 812.

At decision block 812, the OS kernel may determine whether the UMSthread 102 has acquired a context lock. In various embodiments, thecontext lock may be in the form of a placement of a non-default valueinto the lock bit 606 of the UMS lock 602 (FIG. 6). If the OS kerneldetermines that the UMS thread 102 has acquired a context lock (“yes” atdecision block 812), which indicates that the UMS thread 102 is actuallybeing executed in kernel mode 112, the process 800 may proceed to block806.

At block 806, the APC may be processed as part of standard threadprocessing, such as the processing of a NT thread. However, if the OSkernel determines that the UMS thread has not acquired a context lock(“no” at decision block 812), the process may proceed to block 814.

At block 814, the UMS thread 102 may acquired a context lock to indicatethat it is being executed in kernel mode 112 (since the UMS thread 102is not being executed by a primary thread). Subsequently, the process800 may proceed to block 806. At block 806, the APC may be processed aspart of standard thread processing, such as the processing of a NTthread.

Returning to decision block 810, if the OS kernel determines that theuser portion 118 is running in primary thread 106 (“yes” at decisionblock 810), the process 800 may proceed to decision block 816.

At decision block 816, the OS kernel may determine whether theidentification 608 (FIG. 6) of the use portion 118 has been placed intothe CID field 604 (FIG. 6) of the primary thread 106 as part of thecurrent process. If the OS kernel determines that the identification 608has not been placed into the CID field 604 as part of the currentprocess (“no” at the decision block 816), the process 800 may proceed todecision block 818. However, if the OS kernel determines that theidentification 608 has been placed into the CID field 604 of the primarythread 106 as part of the current process, (“yes” at decision block816), the process 800 may proceed to decision block 820.

Returning to decision block 818, the OS kernel may determine whether thecurrent process is being deleted from the system. If the OS kerneldetermines that the current process is being deleted (“yes” at decisionblock 818), the process 800 may proceed to block 822.

At block 822, the OS kernel may determine that the primary thread 106has already exited user mode 114 prior to the user thread descriptorloading to the primary thread 106 is completed as part of a UMS switch.Subsequently, the process 800 may proceed to block 806.

At block 806, the APC may be processed as part of standard threadprocessing, such as the processing of a NT thread. Returning to decisionblock 818, if the OS kernel determines that the current process is not adeletion process (“no” at decision block 818), the process may proceedto decision block 820.

At decision block 820, a user mode scheduler, such as the user modescheduler 206, may determine whether the primary thread 106 is in usermode 114. If the user mode scheduler determines that the primary thread106 is in user mode 114 (“yes” at decision block 820), the process 800may proceed to decision block 824 in FIG. 8 b.

At decision block 824, the user mode scheduler may determine whether theuser portion 118 is running on the primary thread 106. In other words,the user mode scheduler may determine whether the portion 122 of theprimary thread 106 may have switched with the user portion 118 forexecution in user mode 114. If the user mode scheduler determines thatthe user portion 118 is not running on the primary thread 106 (“no” atdecision block 820), the process may proceed to block 826. At block 826,the user mode scheduler 102 may return the primary thread 106 to usermode 114.

However, if the user mode scheduler determines that the user portion 118is running on the primary thread 106 (“yes” at decision block 824), theprocess 800 may proceed to block 828.

At block 828, the register context associated with the user portion 118may be captured. In various embodiments, the captured registered contextmay include both asynchronous and synchronous contexts. At block 830,the user portion 118 may be queued to a completion list, which isdescribed above. At block 832, the user thread descriptor 202 of theprimary thread 106 and the control procedure call (PC) may be reset tosystem-defined user entry points. In various embodiments, the primarythread 106 may return the context information from its kernel portion120 to the user portion 118 of the UMS thread 102. At block 834, the OSkernel may signal the UMS thread 102 to continue APC processing. At thispoint, the process 800 may also continue to block 826, where the usermode scheduler 102 may return the primary thread 106 to user mode 114.

Returning to decision block 820, if the user mode scheduler determinesthat the primary thread 106 is not in user mode 114 (“no” at decisionblock 820), the process may proceed to decision block 836.

At decision block 836, the OS kernel may determine whether the primarythread 106 is the middle of a directed switch with the UMS thread 102.In other words, the kernel portion 120 of the primary thread 106 isbeing switched with the user portion 118 of the UMS thread 102. If theOS kernel determines that a directed switch is not taking place (“no” atdecision block 836), the process 800 may proceed to block 828, where theprocess 800 will progress as previously described.

However, if the OS kernel determines that a directed switch is takingplace (“yes” at decision block 836), the process 800 may proceed toblock 842. At block 842, the UMS thread 102 may be synchronized with theprimary thread 106. At decision block 844, the OS kernel may determinewhether the joining of the primary thread 106 to the UMS thread 102 iscompleted. In other words, the OS kernel may determine whether the waitfor the primary thread 106 to complete the transfer of states from theUMS thread 102 is finished. If the OS kernel determines that the joiningof the primary thread 106 is completed (“yes” at decision block 844),the process 800 may further proceed to block 846. However, if the OSkernel determines that the joining of the primary thread 106 is notcomplete (“no” at decision block 846), the process 800 may loop back toblock 842, where additional synchronization may be performed. Thislooping may be performed until synchronization is complete.

At block 846, the context information from the primary thread 106 may bemigrated to a backup store of the UMS thread 102. In variousembodiments, as described in FIG. 5 d, the context information from theuser portion 120 may be “pulled” into staging area 524 of the kernelportion 116. At block 848, the UMS thread 102 may be “unparked,” thatis, waken up from a waiting state. Once awoken, the context informationof the UMS thread 102 may be replaced with the context information fromthe backup store. At block 850, the OS kernel may continue APCservicing. In some embodiments, the APC may be executed on the UMSthread 102 with the replaced context information.

FIG. 9 is a flow diagram illustrating an exemplary process 900controlling the execution of a UMS thread in user mode, in accordancewith various embodiments. It will be appreciated that while the process900 is illustrated using the UMS thread 102 and the primary thread 106,the process 900 may be applicable to other UMS threads and primarythreads.

At block 902, the user mode scheduler may acquire the client identifier(CID), such as CID field 604 of a UMS thread 102. In variousembodiments, the CID field 604 may be part of a UMS lock 602 thatfurther includes a lock bit 606.

At decision block 904, it may be determined whether the context lock ofthe UMS thread 102 is successfully acquired by the user mode scheduler.In other words, it is determined whether the user mode scheduler is ableto acquire the lock bit 606 of the UMS lock 902. If it is determinedthat the user mode scheduler is not able to acquire the context lock(“no” at decision block 904), the process 900 may proceed to block 906.At block 906, the user mode scheduler may report an unsuccessful UMSexecution to a caller. However, if it is determined that the user modescheduler is able to acquire the context lock (“yes” at decision block904), the process 900 may proceed to decision block 908.

At decision block 908, the user mode scheduler may determine whether thecontext is terminated or suspended. If the user mode schedulerdetermines that the context is terminated or suspended (“yes” atdecision block 906), the process 900 may proceed to block 906. At block906, the user mode scheduler may report an unsuccessful UMS execution tothe caller. However, if the user mode scheduler determines that thecontext is not terminated or suspended, (“no” at decision block 906),the process 900 may proceed to block 910.

At block 910, the primary thread 106 may load to the user threaddescriptor 134 from the UMS thread 102. In various embodiments, theprimary thread 106 may load the user thread descriptor of the UMS thread102 using the pointers described in FIG. 3.

At decision block 912, the user mode scheduler may determine whether theUMS thread 102 is kernel locked. In various embodiments, the UMS thread102 is kernel locked if the lock bit 606 indicates that the UMS thread102 is not of interest to the kernel 112. For example, in at least oneembodiment, the lock bit 606 may have a default value if the UMS thread102 is not locked by the OS kernel. If the user mode schedulerdetermines that the UMS thread 102 is kernel locked (“yes” at decisionblock 912), the process 900 may proceed to block 914. At block 914, theprimary thread 106 may release the CID field 604 of the UMS thread 102.In other words, the identification 608 of the primary thread 106 may beremoved from the CID field 604 of the UMS thread 102. Subsequently, theprocess 900 may proceed to block 906. At block 906, the user modescheduler may report an unsuccessful UMS execution to the OS kernel.

However, if the user mode scheduler determines that the UMS thread 102is not kernel locked (“no” at decision block 912), the process mayproceed to block 916. At block 916, the UMS thread 102 may record thecontext reference of the primary thread. In other words, theidentification 608 (FIG. 6) of the primary thread 106 may be recorded inthe CID field 604 field of the UMS thread context 102. At block 918, theCPU context of the UMS thread 102, that is, the user portion 118, may beloaded for execution in user mode 114.

FIG. 10 is a flow diagram illustrating an exemplary process 1000controlling the yield of a UMS thread in user mode, in accordance withvarious embodiments. It will be appreciated that while the process 1000is illustrated using the UMS thread 102 and the primary thread 106, theprocess 1000 may be applicable to other UMS threads. It will beappreciated that a yield may be performed by a UMS thread 102 in usermode 114 to give up its execution via the primary thread 106 so that theprimary thread 106 may provide execution to another UMS thread.

At block 1002, the CPU context of the UMS thread 102, as present in theuser portion 122 of the primary thread 106, may be saved back into theuser portion 118 of the UMS thread 102. At block 1004, the user threaddescriptor 134 of the primary thread 106 may be reloaded. In otherwords, the user thread descriptor 134 of primary thread 106 may be resetto its original state.

At block 1006, the primary thread 106 may release the CID field 604 ofthe UMS thread 102. In other words, the identification 608 of theprimary thread 106 may be removed from the CID field 604 of the UMSthread 102 context. At block 1008, the user mode scheduler may load anew CPU context into the primary thread 106.

Exemplary Computing Device

FIG. 11 illustrates a representative computing device 1100 that may beused to implement the directed switch and optimization techniques andmechanisms described herein. For example, the multi-processorenvironment 100 (FIG. 1) may be implemented on the representativecomputing device 1100. However, it is readily appreciated that thevarious embodiments of the directed switch implementation and supporttechniques and mechanisms may be implemented in other computing devices,systems, and environments. The computing device 1100 shown in FIG. 11 isonly one example of a computing device and is not intended to suggestany limitation as to the scope of use or functionality of the computerand network architectures. Neither should the computing device 1100 beinterpreted as having any dependency or requirement relating to any oneor combination of components illustrated in the example computingdevice.

In a very basic configuration, computing device 1100 typically includesat least one processing unit 1102 and system memory 1104. Depending onthe exact configuration and type of computing device, system memory 1104may be volatile (such as RAM), non-volatile (such as ROM, flash memory,etc.) or some combination of the two. System memory 1104 typicallyincludes an operating system 1106, one or more program modules 1108, andmay include program data 1110. The operating system 1106 include acomponent-based framework 1112 that supports components (includingproperties and events), objects, inheritance, polymorphism, reflection,and provides an object-oriented component-based application programminginterface (API), such as, but by no means limited to, that of the .NET™Framework manufactured by Microsoft Corporation, Redmond, Wash. Thedevice 1100 is of a very basic configuration demarcated by a dashed line1114. Again, a terminal may have fewer components but will interact witha computing device that may have such a basic configuration.

Computing device 1100 may have additional features or functionality. Forexample, computing device 1100 may also include additional data storagedevices (removable and/or non-removable) such as, for example, magneticdisks, optical disks, or tape. Such additional storage is illustrated inFIG. 11 by removable storage 1116 and non-removable storage 1118.Computer storage media may include volatile and nonvolatile, removableand non-removable media implemented in any method or technology forstorage of information, such as computer readable instructions, datastructures, program modules, or other data. System memory 1104,removable storage 1116 and non-removable storage 1118 are all examplesof computer storage media. Computer storage media includes, but is notlimited to, RAM, ROM, EEPROM, flash memory or other memory technology,CD-ROM, digital versatile disks (DVD) or other optical storage, magneticcassettes, magnetic tape, magnetic disk storage or other magneticstorage devices, or any other medium which can be used to store thedesired information and which can be accessed by computing device 1100.Any such computer storage media may be part of device 1100. Computingdevice 1100 may also have input device(s) 1120 such as keyboard, mouse,pen, voice input device, touch input device, etc. Output device(s) 1122such as a display, speakers, printer, etc. may also be included. Thesedevices are well known in the art and are not discussed at length here.

Computing device 1100 may also contain communication connections 1124that allow the device to communicate with other computing devices 1126,such as over a network. These networks may include wired networks aswell as wireless networks. Communication connections 1124 are oneexample of communication media. Communication media may typically beembodied by computer readable instructions, data structures, programmodules, etc.

It is appreciated that the illustrated computing device 1100 is only oneexample of a suitable device and is not intended to suggest anylimitation as to the scope of use or functionality of the variousembodiments described. Other well-known computing devices, systems,environments and/or configurations that may be suitable for use with theembodiments include, but are not limited to personal computers, servercomputers, hand-held or laptop devices, multiprocessor systems,microprocessor-base systems, set top boxes, game consoles, programmableconsumer electronics, network PCs, minicomputers, mainframe computers,distributed computing environments that include any of the above systemsor devices, and/or the like.

The use of directed switches for primary threads and the UMS thread mayenable pure user mode switching. The use of pure user mode switching mayprovide an architecture and/or platform for applications, such as highperformance enterprise applications, with the ability to better controlthread execution and concurrency. Moreover, the use of directed switcheson a multi-processor platform may enable applications to scaleefficiently. With the use of directed switches and support mechanismsdescribed herein, applications may have the ability to utilize allsystem services and/or application programming interface (API) on UMSthreads transparently, that is, in the same manner as standard NTthreads without compatibility issues.

Conclusion

In closing, although the various embodiments have been described inlanguage specific to structural features and/or methodological acts, itis to be understood that the subject matter defined in the appendedrepresentations is not necessarily limited to the specific features oracts described. Rather, the specific features and acts are disclosed asexemplary forms of implementing the claimed subject matter.

We claim:
 1. A method, comprising: receiving an original asynchronousprocedure call (APC) on a kernel portion of a user mode schedulable(UMS) thread during an execution of the UMS thread, the UMS threadincluding a UMS thread user portion for execution in the user mode and aUMS thread kernel portion for execution in a kernel mode that areindependently managed by an application, in which execution switchbetween the UMS thread user portion and the UMS thread kernel portionduring the execution of the UMS thread is performed via a primary threadthat exchange context information with the UMS thread, the primarythread having a primary thread user portion and a primary thread kernelportion that are managed in unison by the application; determining astatus of the UMS thread that is being processed in a multi-processorenvironment, wherein the status includes one of the UMS thread executingin kernel mode, the UMS thread executing via the primary thread in usermode, the UMS thread engaging in a context migration with the primarythread, the UMS thread being idle, the UMS thread engaging in a contextloading with the primary thread, or the UMS thread engaging in a contextsaving with the primary thread; and processing the original APC on theUMS thread based on the status of the UMS thread.
 2. The method of claim1, wherein the primary thread includes first context information from aCPU, and the UMS thread includes second context information from amemory.
 3. The method of claim 1, wherein the determining includesdetermining that the UMS thread is executing in kernel mode, and whereinthe execution further comprise executing the original APC on the UMSthread kernel portion to one of retrieve or modify context informationof the kernel portion.
 4. The method of claim 1, further comprisinginserting an identifier (ID) of the primary thread into a clientidentifier (CID) field of the UMS thread prior to executing the UMSthread via the primary thread in user mode, and wherein the determiningincludes determining that the UMS thread is associated with the primarythread based on the CID.
 5. The method of claim 1, wherein thedetermining includes determining that the UMS thread is idle, andwherein the processing further comprises: placing a lock on the UMSthread user portion when the UMS thread is not being executed, the lockpreventing the UMS thread from being executed in one of kernel mode oruser mode; and executing the original APC on the UMS thread user portionto one of retrieve or modify context information of the UMS thread userportion.
 6. The method of claim 1, wherein the determining includesdetermining that the UMS thread is executing via the primary thread inuser mode, and wherein the processing further comprises: firing adisassociate APC at the primary thread to cause the primary thread toabandon execution in user mode and return to kernel mode; returningcontext information from the primary thread kernel portion to the UMSthread user portion; and executing the original APC on the UMS threaduser portion to one of retrieve or modify the context information of theUMS thread user portion.
 7. The method of claim 1, wherein thedetermining includes determining that the UMS thread is engaging in acontext migration with the primary thread, and wherein the processingfurther comprises: pulling new context information from the primarythread kernel portion to a staging area in the UMS thread kernel portionvia an original APC; and executing the original APC on the UMS threaduser portion to one of retrieve or modify the new context information ofthe UMS thread kernel portion.
 8. The method of claim 1, wherein thedetermining includes determining that the UMS thread is engaging in acontext migration with the primary thread, and wherein the processingfurther comprises: providing an indicator to signal that the primarythread is ready to transfer new context information to the UMS thread inresponse to an disassociate APC that is generated based on an originalAPC; pulling new context information from the primary thread kernelportion to a staging area in the UMS thread kernel portion the anoriginal APC; replacing old context information of the UMS thread kernelportion with the new context information in the staging area; andexecuting the original APC on the UMS thread user portion to one ofretrieve or modify the new context information of the UMS thread kernelportion.
 9. The method of claim 1, wherein the determining includesdetermining that the UMS thread is engaging in a context loading withthe primary thread, and wherein the processing further comprises:completing a transfer of context information from the UMS thread userportion to the primary thread user portion that is in progress inresponse to a disassociate APC, the disassociate APC being generatedbased on an original APC; and executing the original APC on the UMSthread user portion to one of retrieve or modify the context informationof the UMS thread user portion.
 10. The method of claim 1, wherein thedetermining includes determining that the UMS thread is engaged incontext saving with the primary thread, and wherein the processingfurther comprises: completing a replacement of first context informationin the UMS thread user portion with second context information from theprimary thread user portion in response to a disassociate APC, thedisassociate APC being generated based on an original APC; executing theoriginal APC on the UMS thread user portion to one of retrieve or modifythe first context information of the UMS thread user portion; andrepeating the replacement of the first context information in the UMSthread user portion with the second context information from the primarythread user portion.
 11. The method of claim 1, wherein the determiningincludes determining that the UMS thread is engaged in context savingwith the primary thread as a result of a page fault, and wherein theprocessing further comprises: terminating a replacement of first contextinformation in the UMS thread user portion with second contextinformation from the primary thread user portion in response to adisassociate APC, the disassociate APC being generated based on anoriginal APC; restoring the first context information of the UMS userportion to states prior to the replacement; executing the original APCon the UMS thread user portion to one of retrieve or modify the firstcontext information of the UMS thread user portion; and processing theprimary thread kernel portion in kernel mode instead of the primarythread user portion in user mode as a result of the page fault.
 12. Themethod of claim 1, wherein the UMS thread includes a data bit toindicate that the UMS thread is executing in the kernel mode, andwherein the determining includes determining that the UMS thread isexecuting in the kernel mode based on a value of the data bit.
 13. Themethod of claim 1, wherein the UMS thread includes an identifier fieldto indicate that the UMS thread is switched with the primary thread, andwherein the determining includes determining that the UMS thread isswitched with the primary thread based on a value of the identifierfield.
 14. A computer readable memory storing computer-executableinstructions that, when executed, cause one or more processors toperform acts comprising: receiving an original asynchronous procedurecall (APC) on a kernel portion of a user mode schedulable (UMS) thread;determining that the UMS thread is engaging in a context migration witha primary thread; providing an indicator to signal that the primarythread is ready to transfer new context information to the UMS thread inresponse to an disassociate APC that is generated based on an originalAPC; pulling new context information from a primary thread kernelportion to a staging area in a UMS thread kernel portion of the anoriginal APC; replacing old context information of the UMS thread kernelportion with the new context information in the staging area; andexecuting the original APC on a UMS thread user portion to one ofretrieve or modify the new context information of the UMS thread kernelportion.
 15. The computer readable memory of claim 14, wherein the UMSthread includes a data bit indicating that the UMS thread is executingin a kernel mode when the UMS thread portion is executing in the kernelmode.
 16. The computer readable memory of claim 14, wherein the UMSthread includes an identifier field to indicate that the UMS thread isswitched with the primary thread when a switch occurs.
 17. A computerreadable memory storing computer-executable instructions that, whenexecuted, cause one or more processors to perform acts comprising:receiving an original asynchronous procedure call (APC) on a kernelportion of a user mode schedulable (UMS) thread; determining that theUMS thread is engaged in context saving with a primary thread;completing a replacement of first context information in a UMS threaduser portion with second context information from a primary thread userportion in response to a disassociate APC, the disassociate APC beinggenerated based on an original APC; executing the original APC on theUMS thread user portion to one of retrieve or modify the first contextinformation of the UMS thread user portion; and repeating thereplacement of the first context information in the UMS thread userportion with the second context information from the primary thread userportion.
 18. The computer readable memory of claim 17, wherein the UMSthread includes a data bit indicating that the UMS thread is executingin a kernel mode when the UMS thread portion is executing in the kernelmode.
 19. The computer readable memory of claim 17, wherein the UMSthread includes an identifier field to indicate that the UMS thread isswitched with the primary thread when a switch occurs.